package com.tencent.welink.util;

import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.security.SecureRandom;
import java.util.Random;

public class RandomUtil {
    private static final Logger logger = LogManager.getLogger(RandomUtil.class);
    
    private static Random randomInt = new Random(System.currentTimeMillis());

    /**
     * 根据base64格式随机生成一段字符串
     * @param length 需要生成的字符串长度
     * @return
     */
    public static String getBase64Random(int length) {
        String base = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789*-_";
        Random random = new Random();
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < length; i++) {
            int number = random.nextInt(base.length());
            sb.append(base.charAt(number));
        }
        String res = sb.toString();

        //logger.info("INFO: 获取随机字符串：" + res);

        return res;

    }

    /**
     * 自定义内容随机生成一串字符串
     * @param length 需要生成的字符串长度
     * @param s 字符串产生自的字符池子
     * @return String
     */
    public static String getRandomStr(int length, String s) {

        String base = "";

        if (StringUtils.isEmpty(s)) {
            base = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        } else {
            base = s;
        }

        Random random = new SecureRandom();
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < length; i++) {
            int number = random.nextInt(base.length());
            sb.append(base.charAt(number));
        }

        return sb.toString();

    }

    public static int getRandomInt(){
        Random random = new SecureRandom();
        return random.nextInt(Integer.MAX_VALUE);
    }
    
    public static Integer getUnInt(){
        return randomInt.nextInt(Integer.MAX_VALUE);
    }
    
    public static void main(String[] args) {
        System.out.println(getRandomStr(16,null));
    }

}
